package org.gvp.generator.core.writer.impl;

import org.gvp.generator.constant.ImportConstant;
import org.gvp.generator.core.writer.AbstractFileWriter;
import org.gvp.generator.props.CreateProps;
import org.gvp.generator.props.ProjectProps;
import org.gvp.generator.table.TableInfo;

import java.nio.file.Path;
import java.nio.file.Paths;

public class EntityFileWriter extends AbstractFileWriter {
    @Override
    public void execute(TableInfo tableInfo, ProjectProps project) {
        String classDefine ="public class %s{".formatted(tableInfo.getUpperName()) ;
        String annotationDefine = """
                @Data
                @TableName(schema = "%s",value = "%s")
                """.formatted(tableInfo.getDatabaseName(),tableInfo.getTableName());

        channel.write(project.getBasePackageImport() + project.getEntityPackage() + ";");
        channel.writeEmptyLine();
        channel.writeEmptyLine();
        channel.writeNewline(ImportConstant.MYBATIS_PLUS);
        channel.writeNewline(ImportConstant.LOMBOK_DATA);
        channel.writeEmptyLine();
        channel.writeNewline(ImportConstant.LOCAL_DATE);

        channel.write(getClassComment(tableInfo.getRemark() + "实体类"));
        channel.write(annotationDefine);
        channel.writeNewline(classDefine);

        tableInfo.getColumns().forEach(column -> {
            if (column.isAutoIncrement()){
                String columnDefine = """
                        /**
                        * %s
                        */
                        @TableId(type = IdType.AUTO,value = "`%s`")
                        private %s %s;
                    """.formatted(column.getRemark(),column.getColumnName(), column.getJavaType(), column.getLowerName());
                channel.write(columnDefine);
                return;
            }
            String columnDefine = """
                        /**
                        * %s
                        */
                        @TableField(value = "`%s`")
                        private %s %s;
                    """.formatted(column.getRemark(),column.getColumnName(), column.getJavaType(), column.getLowerName());
            channel.write(columnDefine);
        });
        channel.writeNewline("}");
    }
    @Override
    public boolean isWriterFile(CreateProps create) {
        return create.isCreateEntity();
    }
    @Override
    public Path getFilePath(String basePath, ProjectProps project, String fileName) {
        return Paths.get(basePath, project.getEntityPackage(), fileName + ".java");

    }
}
